package com.ksm.zyz.controller;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ksm.zyz.sys.entity.BaseSupplierInfo;
import com.ksm.zyz.sys.serverservice.sXinXiBiaoService;
import com.ksm.zyz.sys.service.baseSupplierInfoService;
import com.ksm.zyz.sys.service.orderGoodsInfoService;

/**
 * @author liuhao
 *
 */
@Controller
@RequestMapping(value="Syncss" ,produces = { "application/json;charset=UTF-8" })
public class GuoduServerController{
	@Autowired
	private  sXinXiBiaoService xService;
	@Autowired
	private   orderGoodsInfoService orderGoodsInfoService;
	@Autowired
	private  baseSupplierInfoService BaseSupplierInfoService;
	

	
	   @RequestMapping(value ="syncss",method=RequestMethod.POST)
	    @ResponseBody
   public String tongbu(String json, HttpServletRequest req){
      
	try {
		System.out.println("@@@@@@@@@@@"+json);
		String json1=req.getParameter("json");
		System.out.println("#############"+json1);
		ObjectMapper mapper=new ObjectMapper();
		/*String json=httpURLConnectionPOST("ming=1");*/
	    
		Map<String, Map<String,List>> map1 = mapper.readValue(json, Map.class);
		System.out.println(map1);
		System.out.println(map1.get("updatemap"));
		if(map1.get("updatemap")!=null)
		{

			if(map1.get("updatemap").get("BaseSupplierInfo")!=null)
			{
				for(int i=0;i<map1.get("updatemap").get("BaseSupplierInfo").size();i++)
				{
					String ii= mapper.writeValueAsString(map1.get("updatemap").get("BaseSupplierInfo").get(i));
					BaseSupplierInfo iif=mapper.readValue(ii,BaseSupplierInfo.class);
					//iif.setSmId(ming);
					int r=BaseSupplierInfoService.selectInfoCount(iif);
	                if(r==1)
	                {  
	                	int s=BaseSupplierInfoService.updateInfo(iif);		       	  
	                }
	                else if(r==0)
	                {
	                	int s=BaseSupplierInfoService.insertSelective(iif);			
	                }	
	                else
	                {
	                	int s=BaseSupplierInfoService.deleteInfo(iif);
						if(s>0)
						{
							BaseSupplierInfoService.insertSelective(iif);	
						}
	                }
				}
			}
			
		}
		if(map1.get("insertmap")!=null)
		{
			
			if(map1.get("insertmap").get("BaseSupplierInfo")!=null)
			{
				for(int i=0;i<map1.get("insertmap").get("BaseSupplierInfo").size();i++)
				{
					String ii= mapper.writeValueAsString(map1.get("insertmap").get("BaseSupplierInfo").get(i));
					BaseSupplierInfo iif=mapper.readValue(ii,BaseSupplierInfo.class);
					//iif.setSmId(ming);
					int r=BaseSupplierInfoService.selectInfoCount(iif);
	                if(r>0)
	                { 
	                	int s=BaseSupplierInfoService.deleteInfo(iif);
	                	if(s>0)
	                	{
	                	 BaseSupplierInfoService.insertSelective(iif);
	                	}	
	                }
	                else if(r==0)
	                {
	                  int s=BaseSupplierInfoService.insertSelective(iif);			
	                }					
				}
			}
		}
		if(map1.get("deletemap")!=null)
		{
			
				if(map1.get("deletemap").get("BaseSupplierInfo")!=null)
				{
					for(int i=0;i<map1.get("deletemap").get("BaseSupplierInfo").size();i++)
					{
						String ii= mapper.writeValueAsString(map1.get("deletemap").get("BaseSupplierInfo").get(i));
						BaseSupplierInfo iif=mapper.readValue(ii,BaseSupplierInfo.class);
						//iif.setSmId(ming);
						int r=BaseSupplierInfoService.selectInfoCount(iif);
		                if(r>0)
		                { //数据库中有此条数据  暂时没有回应         
		                	int s=BaseSupplierInfoService.deleteInfo(iif);	
		                }
		                					
					}
				}
		}
		
	    return "success";   
	} catch (JsonParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return "JsonParseException";
	} catch (JsonMappingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return "JsonMappingException";
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return "IOException";
	}
       
   
   }  
	
	

}
